package com.localjoycommunity.cpdj.project.system.controller;

import java.util.List;

import com.localjoycommunity.cpdj.project.system.domain.TwoNewOrganization;
import com.localjoycommunity.cpdj.project.system.service.ITwoNewOrganizationService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.localjoycommunity.cpdj.framework.aspectj.lang.annotation.Log;
import com.localjoycommunity.cpdj.framework.aspectj.lang.enums.BusinessType;
import com.localjoycommunity.cpdj.project.system.domain.Building;
import com.localjoycommunity.cpdj.project.system.service.IBuildingService;
import com.localjoycommunity.cpdj.framework.web.controller.BaseController;
import com.localjoycommunity.cpdj.framework.web.domain.AjaxResult;
import com.localjoycommunity.cpdj.common.utils.poi.ExcelUtil;
import com.localjoycommunity.cpdj.framework.web.page.TableDataInfo;

/**
 * 楼宇Controller
 *
 * @author leepandar
 * @date 2021-01-19
 */
@RestController
@RequestMapping("/system/building")
public class BuildingController extends BaseController {
    @Autowired
    private IBuildingService buildingService;
    @Autowired
    private ITwoNewOrganizationService organizationService;

    /**
     * 查询楼宇列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Building building) {
        startPage();
        List<Building> list = buildingService.selectBuildingList(building);
        return getDataTable(list);
    }

    /**
     * 查询楼宇列表
     */
    @GetMapping("/app/list")
    public AjaxResult appList(Building building) {
        List<Building> list = buildingService.selectBuildingList(building);
        return AjaxResult.success(list);
    }

    /**
     * 查询楼宇下的两新组织
     */
    @GetMapping("/org/list/{buildingId}")
    public AjaxResult orgList(@PathVariable("buildingId") Integer buildingId) {
        TwoNewOrganization twoNewOrganization = new TwoNewOrganization();
        twoNewOrganization.setBuildingId(buildingId);
        List<TwoNewOrganization> list = organizationService.selectTwoNewOrganizationList(twoNewOrganization);
        return AjaxResult.success(list);
    }

    /**
     * 导出楼宇列表
     */
    @Log(title = "楼宇", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Building building) {
        List<Building> list = buildingService.selectBuildingList(building);
        ExcelUtil<Building> util = new ExcelUtil<Building>(Building.class);
        return util.exportExcel(list, "building");
    }

    /**
     * 获取楼宇详细信息
     */
    @GetMapping(value = "/{buildingId}")
    public AjaxResult getInfo(@PathVariable("buildingId") Long buildingId) {
        return AjaxResult.success(buildingService.selectBuildingById(buildingId));
    }

    /**
     * 新增楼宇
     */
    @Log(title = "楼宇", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Building building) {
        return toAjax(buildingService.insertBuilding(building));
    }

    /**
     * 修改楼宇
     */
    @Log(title = "楼宇", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Building building) {
        return toAjax(buildingService.updateBuilding(building));
    }

    /**
     * 删除楼宇
     */
    @Log(title = "楼宇", businessType = BusinessType.DELETE)
    @DeleteMapping("/{buildingIds}")
    public AjaxResult remove(@PathVariable Long[] buildingIds) {
        return toAjax(buildingService.deleteBuildingByIds(buildingIds));
    }
}
